//
//  MYNewsDetailsBaseVC.m
//  MuYing
//
//  Created by tbb on 2019/8/6.
//  Copyright © 2019 TBB. All rights reserved.
//

#import "MYNewsDetailsBaseVC.h"
#import "MYDynamicCommentVC.h"
#import "MYNewsDetailsModel.h"
#import "MYNewsDetailsHotelVC.h"
#import "MYNewsDetailsScenicspotVC.h"
#import "MYNewsDetailsActionVC.h"
#import "MYNewsDetailsHotTopicVC.h"
#import "MYNewsShopDetailsHotelVC.h"
#define IMAGE_HEIGHT kYQPScreenWidth * (225/375.0)
#define NAVBAR_COLORCHANGE_POINT (IMAGE_HEIGHT - Height_NavBar*2)

@interface MYNewsDetailsBaseVC ()
@property (nonatomic, strong) UIButton * sharedBtn;

@property (nonatomic, strong) MYDynamicDetailsBottonView * bottonView;

@property (strong, nonatomic) NSMutableArray *sortArray;

@property (nonatomic, strong) NSMutableArray * bannerurlArr;
@end

@implementation MYNewsDetailsBaseVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _setupNavView];
    //原点（0，64）开始
    self.edgesForExtendedLayout = UIRectEdgeTop;
    self.view.backgroundColor = UIColor.whiteColor;
    [self wr_setNavBarBackgroundAlpha:0.0];
    self.navigationController.navigationBar.translucent = YES;

    self.sortArray = NSMutableArray.new;
    
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent = YES;
    //[[IQKeyboardManager sharedManager] setEnable:YES];
}

//保证进入下个界面nav正常显示
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.translucent = NO;
}

-(void)setupView {
    [super setupView];
    self.listTableView.backgroundColor = HexColor(0xFAFAFA);
    self.listTableView.frame = CGRectMake(0, 0, kYQPScreenWidth, kYQPScreenHeight-kHeight_Botton_SafeArea-49);
    
    [self.view addSubview:self.bottonView];
    [self.bottonView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.height.mas_equalTo(49);
    }];

    kYQREGISTERTABLEVIEWCHeaderFooter([MYShopDetailsHeaderView class])
    kYQREGISTERTABLEVIEWCELL([MYNewsDetailsHotelTitleCell class])
    kYQREGISTERTABLEVIEWNIBCELL(@"MYNewsDetailsTextCell")
    kYQREGISTERTABLEVIEWNIBCELL(@"MYNewsDetailsTimeCell")
    kYQREGISTERTABLEVIEWNIBCELL(@"MYNewsDetailsLocationCell")
    kYQREGISTERTABLEVIEWNIBCELL(@"MYShopDetailsCommentCell")
    
    kYQREGISTERTABLEVIEWCHeaderFooter([MYShopDetailsTitleHeaderView class])
    kYQREGISTERTABLEVIEWCHeaderFooter([MYShopDetailsCommentHeaderView class])
    
    kYQREGISTERTABLEVIEWNIBCELL(@"MYNewsGoodsCell")
    self.listTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getNewsData];
    }];
    [self getNewsData];
}
//
-(void)getNewsData {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"informationCategoryId"] = self.informationCategoryId;
    parameters[@"informationId"] = self.informationId;
    parameters[@"latitude"] = [Config currentConfig].latitude;
    parameters[@"longitude"] = [Config currentConfig].longitude;
    YQSHOWLOADINGTIPS(@"");
    [YQPNetWorkingManager sendPostRequest:kGET_NEWS_INFO_URL parameters:parameters success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        YQHIDELOADINGTIPS
        [self stopRefresh];
        if (responseObject.code == 0) {
            [self.bannerurlArr removeAllObjects];
            self.detailsModel = [MYNewsDetailsModel modelWithJSON:responseObject.data];
            for (MYNewsDetailsImageModel *imageModel in self.detailsModel.informationImgList) {
                if (imageModel.imageUrl) {
                    [self.bannerurlArr addObject:imageModel.imageUrl];
                }
            }
            self.bottonView.callBtn.selected = self.detailsModel.isLike;
            self.bottonView.likeBtn.selected = self.detailsModel.isCollect;
            [self.bottonView.callBtn setTitle:[NSString stringWithFormat:@"%@ %@",kYQLS(@"点赞"),self.detailsModel.likeNumber] forState:(UIControlStateNormal)];
            [self.bottonView.likeBtn setTitle:[NSString stringWithFormat:@"%@ %@",kYQLS(@"收藏"),self.detailsModel.collectNumber] forState:(UIControlStateNormal)];
            [self.bottonView.commentBtn setTitle:[NSString stringWithFormat:@"%@ %@",kYQLS(@"评价"),self.detailsModel.commentNumber] forState:(UIControlStateNormal)];
            if ([self.detailsModel.relevanceCommodityId isEqualToString:@"0"] || self.detailsModel.relevanceCommodityId.length == 0) {
                self.buyBtn.hidden = YES;
            }else{
                self.buyBtn.hidden = NO;

            }
            [self.listTableView reloadData];
        }else {
            [self stopRefresh];
            YQUITIPSERROR(responseObject.msg)
        }
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        YQUITIPSERROR(error)
        YQHIDELOADINGTIPS
    }];
    
}

-(void)callAction:(UIButton *)sender {
    YQSHOWLOADINGINVIEW
    NSString *url = [NSString stringWithFormat:@"%@%@",kPOST_INFORMATION_ADD_LIKE_URL,self.informationId];
    [YQPNetWorkingManager sendPostRequest:url parameters:nil success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        YQHIDELOADINGTIPS
        if (responseObject.code == 0) {
            YQUITIPSSUCCEED(responseObject.msg)
            self.detailsModel.isLike = !self.detailsModel.isLike;
            self.bottonView.callBtn.selected = self.detailsModel.isLike;
            [self.bottonView.callBtn setTitle:[NSString stringWithFormat:@"%@ %d",kYQLS(@"点赞"),self.detailsModel.likeNumber.intValue + 1] forState:(UIControlStateNormal)];

        }else{
            YQUITIPSERROR(responseObject.msg)
        }
        
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        YQHIDELOADINGTIPS
    }];
    
}
-(void)collectAction:(UIButton *)sender {
    YQSHOWLOADINGINVIEW
    NSString *url = [NSString stringWithFormat:@"%@%@/%@",kPOST_ADD_USER_COLLECTIONS_URL,self.informationId,@"3"];
    [YQPNetWorkingManager sendPostRequest:url parameters:nil success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        YQHIDELOADINGTIPS
        if (responseObject.code == 0) {
            YQUITIPSSUCCEED(responseObject.msg)
            self.detailsModel.isCollect = !self.detailsModel.isCollect;
            self.bottonView.likeBtn.selected = self.detailsModel.isCollect;
        }else{
            YQUITIPSERROR(responseObject.msg)
        }
        
    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        YQUITIPSERROR(error)
        YQHIDELOADINGTIPS
    }];
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 2;
    }else if (section == 1) {
        return 3;
    }else if (section == 2) {
        return self.detailsModel.moreList.count;
    }else{
        if (self.detailsModel.informationCommentList.count > 2) {
            return 2;
        }
        return self.detailsModel.informationCommentList.count;
    }
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            MYNewsDetailsHotelTitleCell *titleCell = [tableView dequeueReusableCellWithIdentifier:@"MYNewsDetailsHotelTitleCell"];
            
            [titleCell.callBtn setTitle:[NSString stringWithFormat:@"%@\n%@",self.detailsModel.likeNumber,kYQLS(@"点赞")] forState:(UIControlStateNormal)];
            [titleCell.collectionBtn setTitle:[NSString stringWithFormat:@"%@\n%@",self.detailsModel.collectNumber,kYQLS(@"收藏")] forState:(UIControlStateNormal)];
            
            [titleCell.lookCountBtn setTitle:[NSString stringWithFormat:@"%@\n%@",self.detailsModel.browseNumber,kYQLS(@"用户看过")] forState:(UIControlStateNormal)];
            titleCell.titleLb.text = self.detailsModel.name;
            titleCell.subLb.text = self.detailsModel.englishName;
            return titleCell;
        }else{
            MYNewsDetailsTextCell *textCell = [tableView dequeueReusableCellWithIdentifier:@"MYNewsDetailsTextCell"];
            textCell.contentLb.text = self.detailsModel.content;
            return textCell;
        }
    }else if (indexPath.section == 1) {
        if (indexPath.row == 0 || indexPath.row == 1) {
            MYNewsDetailsTimeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYNewsDetailsTimeCell"];
            
            if (indexPath.row == 0) {
                cell.timeLb.text = [NSString stringWithFormat:@"%@: %@",kYQLS(@"联系电话"),self.detailsModel.phone];
            }else{
                cell.timeLb.text = [NSString stringWithFormat:@"%@: %@",kYQLS(@"营业时间"),self.detailsModel.businessHours];
            }
            
            return cell;

        }else {
            MYNewsDetailsLocationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYNewsDetailsLocationCell"];
            cell.locationLb.text = [NSString stringWithFormat:@"%@: %@",kYQLS(@"地址"),self.detailsModel.address];
            return cell;
        }
    }else if (indexPath.section == 2) {
        MYNewsDetailsMoreModel *moreModel = nil;
        if (indexPath.row < self.detailsModel.moreList.count) {
            moreModel = self.detailsModel.moreList[indexPath.row];
        }
        MYNewsGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYNewsGoodsCell"];
        cell.titleLb.text = moreModel.title;
        cell.distanceLb.text = moreModel.distanceStr;
        cell.priceLb.text = [NSString stringWithFormat:@"%@",moreModel.price];
        
        [cell.goodsIcon setImageWithURL:YQSTRINGTOURL(moreModel.defaultImg) placeholder:nil];
        return cell;
    }else{
        MYNewsDetailsCommentModel *commetnModel = nil;
        if (indexPath.row < self.self.detailsModel.informationCommentList.count) {
            commetnModel = self.detailsModel.informationCommentList[indexPath.row];
        }
        
        MYShopDetailsCommentCell *commentCell = [tableView dequeueReusableCellWithIdentifier:@"MYShopDetailsCommentCell"];
        commentCell.userNameLb.text = commetnModel.name;
        commentCell.timeLb.text = commetnModel.createTime;
        commentCell.colorLb.text = @"";//@"颜色分类: 红色";
        commentCell.contentLb.text = commetnModel.content;
        return commentCell;
    }

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            return 84;
        }else{
            return [tableView fd_heightForCellWithIdentifier:@"MYNewsDetailsTextCell" configuration:^(MYNewsDetailsTextCell *cell) {
                cell.contentLb.text = self.detailsModel.content;
            }];
        }
    }else if (indexPath.section == 2) {
        return 112;
    }else if (indexPath.section == 1) {
        return 44;
    }else{
        MYNewsDetailsCommentModel *commetnModel = nil;
        if (indexPath.row < self.self.detailsModel.informationCommentList.count) {
            commetnModel = self.detailsModel.informationCommentList[indexPath.row];
        }
        return [tableView fd_heightForCellWithIdentifier:@"MYShopDetailsCommentCell" configuration:^(MYShopDetailsCommentCell *commentCell) {
            commentCell.userNameLb.text = commetnModel.name;
            commentCell.timeLb.text = commetnModel.createTime;
            commentCell.colorLb.text = @"";//@"颜色分类: 红色";
            commentCell.contentLb.text = commetnModel.content;
        }];
    }
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 2) {
        MYNewsDetailsMoreModel *moreModel = nil;
        if (indexPath.row < self.detailsModel.moreList.count) {
            moreModel = self.detailsModel.moreList[indexPath.row];
        }
        switch (moreModel.informationCategoryId) {
            case 1:
            {
                MYNewsDetailsHotelVC *detailsHotelVC = [[MYNewsDetailsHotelVC alloc] init];
                detailsHotelVC.informationId = moreModel.informationId;
                detailsHotelVC.informationCategoryId = @"1";
                kYQPUSH(detailsHotelVC);
            }
                break;
            case 2:
            {
                MYNewsDetailsScenicspotVC * scenicspotVC = [[MYNewsDetailsScenicspotVC alloc] init];
                scenicspotVC.informationCategoryId = @"2";
                scenicspotVC.informationId = moreModel.informationId;
                kYQPUSH(scenicspotVC);
            }
                break;
            case 3:
            {
                MYNewsDetailsActionVC *actionVC = [[MYNewsDetailsActionVC alloc] init];
                actionVC.informationId = moreModel.informationId;
                actionVC.informationCategoryId = @"3";
                kYQPUSH(actionVC);
            }
                break;
                
            default:
            {
                MYNewsDetailsHotTopicVC *vc = [[MYNewsDetailsHotTopicVC alloc] init];
                vc.informationId = moreModel.informationId;
//                vc.informationCategoryId = @"1";
                kYQPUSH(vc);
            }
                break;
        }
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        MYShopDetailsHeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MYShopDetailsHeaderView"];
        header.imageUrlStrArr = self.bannerurlArr.copy;
        return header;
    }else if (section == 2) {
        MYShopDetailsTitleHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MYShopDetailsTitleHeaderView"];
        headerView.titleLb.text = kYQLS(@"附近景点");
        return headerView;
    }else if(section == 1){
        return nil;
    }else{
        MYShopDetailsCommentHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MYShopDetailsCommentHeaderView"];
        headerView.titleLb.text = kYQLS(@"用户评价");
        
//        headerView.countLb.text = [NSString stringWithFormat:@"好评(%zd)",self.detailsModel.goodNumber];//@"好评(39)";
        [headerView.moreBtn addTapAction:^(id  _Nonnull sender) {
            MYDynamicCommentVC *commentVC = [[MYDynamicCommentVC alloc] init];
            commentVC.isNews = YES;
            commentVC.invitationId = self.informationId;
            [self.navigationController pushViewController:commentVC animated:true];
        }];
        return headerView;
    }
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return kYQPScreenWidth * (225 / 375.0);
    }else if (section == 1){
        return 0.001;
    }else if (section == 2){
        return 52;
    }else {
        return 84 - 21;
    }
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetY = scrollView.contentOffset.y;
    
    if (offsetY > NAVBAR_COLORCHANGE_POINT)
    {
        CGFloat alpha = (offsetY - NAVBAR_COLORCHANGE_POINT) / Height_NavBar;
        [self.leftItem setImage:YQIMG(@"nav_back_sIcon") forState:(UIControlStateNormal)];
        [self wr_setNavBarBackgroundAlpha:alpha];
        [self wr_setNavBarTintColor:[[UIColor blackColor] colorWithAlphaComponent:alpha]];
        [self wr_setNavBarTitleColor:[[UIColor blackColor] colorWithAlphaComponent:alpha]];
        self.title = kYQLS(@"详情");
    }
    else
    {
        [self wr_setNavBarBackgroundAlpha:0];
        [self.leftItem setImage:YQIMG(@"ShopDetails_back_icon") forState:(UIControlStateNormal)];
        [self wr_setNavBarTintColor:[UIColor whiteColor]];
        [self wr_setNavBarTitleColor:[UIColor whiteColor]];
        self.title = @"";
    }
}
#pragma mark - UITableViewDelegate, UITableViewDataSource end

#pragma mark --lazy

-(MYDynamicDetailsBottonView *)bottonView {
    if (!_bottonView) {
        _bottonView = [MYDynamicDetailsBottonView viewFromXib];
        _bottonView.backgroundColor = UIColor.whiteColor;
        
        _bottonView.layer.shadowColor = HexColorA(0x000000, 0.6).CGColor;//RGB(70, 69, 69).CGColor;
        //2.设置阴影偏移范围
        _bottonView.layer.shadowOffset = CGSizeMake(0, 2);
        //3.设置阴影颜色的透明度
        _bottonView.layer.shadowOpacity = 0.5;
        
        //4.设置阴影半径
        _bottonView.layer.shadowRadius = 4;
        //5.设置阴影路径
        _bottonView.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.navigationController.navigationBar.bounds].CGPath;
        ws(self);
        [_bottonView.commentBtn addTapAction:^(id  _Nonnull sender) {
            MYDynamicCommentVC *commentVC = [[MYDynamicCommentVC alloc] init];
            commentVC.isNews = YES;
            commentVC.invitationId = self.informationId;
            [weakself.navigationController pushViewController:commentVC animated:true];
        }];
        [_bottonView.callBtn addTarget:self action:@selector(callAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [_bottonView.likeBtn addTarget:self action:@selector(collectAction:) forControlEvents:(UIControlEventTouchUpInside)];
        [_bottonView addSubview:self.buyBtn];
        [self.buyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(_bottonView.commentBtn.mas_left).mas_equalTo(-40);
            make.centerY.mas_equalTo(_bottonView.commentBtn.mas_centerY).mas_equalTo(0);
            make.size.mas_equalTo(CGSizeMake(108, 32));
        }];
    }
    return _bottonView;
}

-(UIButton *)buyBtn {
    if (!_buyBtn) {
        _buyBtn = [UIButton buttonWithType:0];
        [_buyBtn setTitleColor:UIColor.whiteColor forState:(UIControlStateNormal)];
        [_buyBtn setTitle:kYQLS(@"立即预订") forState:(UIControlStateNormal)];
        _buyBtn.titleLabel.font = [UIFont systemFontOfSize:15 weight:(UIFontWeightMedium)];
        _buyBtn.backgroundColor = HexColor(0xF88282);
        _buyBtn.layer.cornerRadius = 16;
        [_buyBtn addTarget:self action:@selector(goToBuyVC:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _buyBtn;
}
-(void)goToBuyVC:(UIButton *)sender {
    
}
-(NSMutableArray *)bannerurlArr {
    if (!_bannerurlArr) {
        _bannerurlArr = [NSMutableArray array];
    }
    return _bannerurlArr;
}

#pragma mark --lazy

-(void)_setupNavView {
    [self.leftItem setImage:YQIMG(@"ShopDetails_back_icon") forState:(UIControlStateNormal)];
    UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 84, 30)];

    
    UIButton *sharedBtn = [UIButton buttonWithType:0];
    [sharedBtn setImage:YQIMG(@"ShopDetails_shread_icon") forState:(UIControlStateNormal)];
    sharedBtn.frame = CGRectMake(54, 0, 30, 30);
    self.sharedBtn = sharedBtn;
    //self.sharedBtn.hidden = YES;
    [rightView addSubview:self.sharedBtn];
    sharedBtn.hidden = YES;
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightView];
    self.navigationItem.rightBarButtonItem = rightItem;
    
}
@end
